/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.bjlytr.license;


import com.bjlytr.license.client.entity.LicenseCheckModel;
import com.bjlytr.license.core.LicenseCreator;
import com.bjlytr.license.entity.LicenseCreatorParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * 证书服务端
 *
 * @author Ledison
 */
public class LicenseApp {

    public static void main(String[] args) {
        LicenseCreatorParam param = new LicenseCreatorParam();
        // 重要参数
        param.setPrivateAlias("privateKey");
        param.setKeyPass("private_password1234");
        param.setStorePass("public_password1234");

        LicenseCheckModel lcm = new LicenseCheckModel();
        Scanner scan = new Scanner(System.in);
        // next方式接收字符串
        System.out.println("开始证书生成：");
        // 是否验证硬件
        while (true) {
            String val = null;
            System.out.println("输入证书生成路径，文件名不能改变。如（D:/keystore/lisence/license.lic）：");
            if (scan.hasNext()) {
                val = scan.next();
            }
            param.setLicensePath(val);
            System.out.println("输入本地私钥文件路径，文件名不能改变，禁止泄露，如有泄露联系李迪！如（D:/keystore/lisence/privateKeys.keystore）：");
            if (scan.hasNext()) {
                val = scan.next();
            }
            param.setPrivateKeysStorePath(val);
            System.out.println("输入证书有效天数：");
            if (scan.hasNext()) {
                val = scan.next();
            }
            long nowTimeStamp = System.currentTimeMillis();
            long oneMinuteBeforeTimeStamp = nowTimeStamp - 60000;
            param.setIssuedTime(new Date(oneMinuteBeforeTimeStamp));
            long expireTimeStamp = oneMinuteBeforeTimeStamp + Long.parseLong(val) * 24 * 60 * 60 * 1000;
            param.setExpiryTime(new Date(expireTimeStamp));
//			if (count == 3) {
//				System.out.println("输入授权服务器IP地址，可传入多个，根据英文逗号进行分割：");
//				if (scan.hasNext()) {
//					val = scan.next();
//				}
//				String[] ips = val.split(",");
//				List<String> ipLst = new ArrayList<>();
//				for (String ip : ips) {
//					ipLst.add(ip);
//				}
//				lcm.setIpAddress(ipLst);
//			}
            System.out.println("是否需要验证授权服务器MAC地址【Y/N】");
            if (scan.hasNext()) {
                val = scan.next();
            }
            while (true) {
                if (!val.equalsIgnoreCase("Y") && !val.equalsIgnoreCase("N")) {
                    System.out.println("输入错误，请输入【Y/N】");
                    if (scan.hasNext()) {
                        val = scan.next();
                    }
                } else {
                    break;
                }
            }
            if (val.equals("N") || val.equals("n")) {
                lcm.setCheckHardware(false);
            } else {
                lcm.setCheckHardware(true);
            }

            if (lcm.isCheckHardware()) {
                System.out.println("输入授权服务器MAC地址，可传入多个，根据英文逗号进行分割：");
                if (scan.hasNext()) {
                    val = scan.next();
                }
                String[] ips = val.split(",");
                List<String> ipLst = new ArrayList<>();
                for (String ip : ips) {
                    ipLst.add(ip);
                }
                lcm.setMacAddress(ipLst);
            }
            param.setLicenseCheckModel(lcm);
            param.setConsumerAmount(1);
            param.setSubject("yoursubject");
            param.setConsumerType("user");
            param.setDescription("Ledison");
            LicenseCreator licenseCreator = new LicenseCreator(param);
            boolean result = licenseCreator.generateLicense();
            if (result) {
                System.err.println("生成成功");
            } else {
                System.err.println("生成失败");
            }
            System.err.println("是否继续添加【Y/N】：");
            if (scan.hasNext()) {
                val = scan.next();
            }
            while (true) {
                if (!val.equalsIgnoreCase("Y") && !val.equalsIgnoreCase("N")) {
                    System.out.println("输入错误，请输入【Y/N】");
                    if (scan.hasNext()) {
                        val = scan.next();
                    }
                } else {
                    break;
                }
            }
            if (val.equalsIgnoreCase("N")) {
                break;
            }
        }
        scan.close();
    }

}
